<template>
  <div>

    <div>
      <el-input style="width: 200px" placeholder="请输入用户名"
                v-model="username"></el-input>
      <el-input style="width: 200px;margin-left: 5px" placeholder="请输入昵称"
                v-model="nickname"></el-input>
      <el-button style="margin: 5px; color: cadetblue" @click="search">搜索</el-button>
      <el-button style="margin-left: 0; color: #c5b8a5" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button style="color: cadetblue" @click="handleAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
        style="margin-left: 10px"
        confirm-button-text='确定'
        cancel-button-text='取消'
        icon="el-icon-info"
        icon-color="#a27e7e"
        title="您确定批量删除这些数据吗？"
        @confirm="handleDeleteBatch"
      >
        <el-button style="color: #cd5c5c" slot="reference" >批量删除<i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>

    <el-table :data="tableData" border stripe :header-cell-style="{backgroundColor: '#F2F2E6'}" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"></el-table-column>
      <el-table-column align="center" prop="id" label="ID" width="65px"></el-table-column>
      <el-table-column align="center" prop="username" label="用户名"></el-table-column>
      <el-table-column align="center" prop="role" label="角色">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_ADMIN'">管理员</el-tag>
          <el-tag type="warning" v-if="scope.row.role === 'ROLE_USER'">普通用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="email" label="邮箱"></el-table-column>
      <el-table-column align="center" prop="nickname" label="昵称" width="120px"></el-table-column>
      <el-table-column align="center" prop="sex" label="性别" width="50px"></el-table-column>
      <el-table-column align="center" label="头像">
        <template slot-scope="scope">
          <v-avatar style="margin-right: 1vw; cursor: pointer"
                    :size="40">
            <img :src="scope.row.profilepic" alt="">
          </v-avatar>
        </template>

      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <a-button style="color: cornflowerblue" @click="handleEdit(scope.row)">授权<i class="el-icon-edit"></i></a-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="60px" size="small">
        <el-form-item label="用户名:">
          <el-input v-model="form.username" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色:">
          <el-select clearable v-model="form.role" placeholder="请选择角色" style="width: 100%">
            <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="昵称:">
          <el-input v-model="form.nickname" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="性别:">
          <el-input v-model="form.sex" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="头像:">
          <el-input v-model="form.profilepic" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱:">
          <el-input v-model="form.email" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button style="color: lightcoral" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
    import request from "../../utils/request";

    export default {
        name: "User",
      data(){
        return{
          username: '',
          nickname: '',
          search_username: '', //搜索用户id
          search_nickname: '', //搜索标题
          tableData: [],
          total: 0,
          pageNum: 1,
          pageSize: 5,
          dialogFormVisible: false,
          multipleSelection: [],
          form: {},
          roles: [],
        }
      },
      created() {
        this.load()
      },
      methods:{
        handleSizeChange(pageSize) {
          this.pageSize = pageSize
          this.load()
        },
        handleCurrentChange(pageNum) {
          this.pageNum = pageNum
          this.load()
        },

        load() {
          // 请求分页查询
          request.get("/user/findPageByUsername", {
            params: {
              PageNum: this.pageNum,
              PageSize: this.pageSize,
              username: this.search_username,
              nickname: this.search_nickname
            }
          }).then(res => {
            if (res.data !== null) {
              this.tableData = res.data.list
              this.total = res.data.total
            } else {
              this.tableData = []
            }
          })

          request.get("/role").then(res => {
            this.roles = res.data
          })
        },

        search() { //搜索
          this.search_username = this.username
          this.search_nickname = this.nickname
          this.load()
        },

        save() {
          request.post("/user", this.form).then(res => {
            if (res) {
              this.$message.success("保存成功!")
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$message.error("保存失败!")
            }
          })
        },

        handleAdd() {
          this.dialogFormVisible = true
          this.form = {}
          this.load()
        },

        handleEdit(row){
          this.form = row
          this.dialogFormVisible = true
        },

        handleDelete(id){
          request.delete("/user/" + id).then(res => {
            if (res) {
              this.$message.success("删除成功!")
              this.load()
            } else {
              this.$message.error("删除失败!")
            }
          })
        },

        handleSelectionChange(val){
          this.multipleSelection = val
        },

        handleDeleteBatch(){
          let ids = this.multipleSelection.map(v => v.id)
          if(ids.length === 0){
            this.$message.error("请选择要删除的数据!")
          }else {
            request.post("/user//del/batch", ids).then(res => {
              if (res) {
                this.$message.success("批量删除成功!")
                this.load()
              } else {
                this.$message.error("批量删除失败!")
              }
            })
          }
        },

        reset() {
          this.username = ''
          this.search_username = ''
          this.nickname = ''
          this.search_nickname = ''
          this.load()
        },
      }

    }
</script>

<style scoped>
  .v-application ul, .v-application ol {
    padding-left: 0 !important;
  }

</style>
